<template>
  <div class="onboarding-container">
    <div class="action-bar">
      <el-button type="primary" icon="el-icon-plus" @click="showCreateDialog">新建入职</el-button>
      <el-button type="success" icon="el-icon-upload" @click="handleImport">批量导入</el-button>
      <el-button icon="el-icon-download" @click="handleExport">导出数据</el-button>
    </div>

    <el-tabs v-model="activeTab" class="tabs-container">
      <el-tab-pane label="基础信息">
        <el-table :data="onboardingList" style="width: 100%">
          <el-table-column prop="name" label="姓名" width="120" />
          <el-table-column prop="employeeId" label="员工编号" width="150" />
          <el-table-column prop="position" label="职位" width="180" />
          <el-table-column prop="entryDate" label="入职日期" width="150" />
          <el-table-column prop="status" label="状态" width="120" />
          <el-table-column label="操作" width="200">
            <template #default="scope">
              <el-button size="small" @click="handleView(scope.row)">查看</el-button>
              <el-button size="small" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
              <el-button size="small" type="danger" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-tab-pane>

      <el-tab-pane label="学历信息">
        <!-- 学历信息表格 -->
      </el-tab-pane>

      <el-tab-pane label="紧急联系人">
        <!-- 紧急联系人表格 -->
      </el-tab-pane>
    </el-tabs>

    <!-- 新建/编辑对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="70%">
      <el-form :model="form" label-width="120px" :rules="rules" ref="formRef">
        <el-tabs>
          <el-tab-pane label="基础信息">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="姓名" prop="name">
                  <el-input v-model="form.name" placeholder="请输入姓名" />
                </el-form-item>
                <el-form-item label="员工编号" prop="employeeId">
                  <el-input v-model="form.employeeId" placeholder="请输入员工编号" />
                </el-form-item>
                <el-form-item label="身份证类型" prop="idType">
                  <el-select v-model="form.idType" placeholder="请选择">
                    <el-option label="居民身份证" value="id_card" />
                    <el-option label="护照" value="passport" />
                    <el-option label="港澳通行证" value="hk_macau" />
                    <el-option label="其他" value="other" />
                  </el-select>
                </el-form-item>
                <el-form-item label="身份证号" prop="idNumber">
                  <el-input v-model="form.idNumber" placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="性别" prop="gender">
                  <el-radio-group v-model="form.gender">
                    <el-radio label="male">男</el-radio>
                    <el-radio label="female">女</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="出生年月" prop="birthDate">
                  <el-date-picker
                      v-model="form.birthDate"
                      type="date"
                      placeholder="选择日期"
                      value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item label="手机号" prop="phone">
                  <el-input v-model="form.phone" placeholder="请输入手机号" />
                </el-form-item>
                <el-form-item label="邮箱" prop="email">
                  <el-input v-model="form.email" placeholder="请输入邮箱" />
                </el-form-item>
                <el-form-item label="政治面貌" prop="politicalStatus">
                  <el-select v-model="form.politicalStatus" placeholder="请选择">
                    <el-option label="群众" value="people" />
                    <el-option label="共青团员" value="league" />
                    <el-option label="中共党员" value="party" />
                    <el-option label="其他" value="other" />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>

          <el-tab-pane label="学历信息">
            <div v-for="(edu, index) in form.educations" :key="index" class="education-item">
              <el-row :gutter="20">
                <el-col :span="8">
                  <el-form-item :label="`学历${index + 1}`" :prop="`educations.${index}.degree`" :rules="rules.degree">
                    <el-select v-model="edu.degree" placeholder="请选择学历">
                      <el-option label="大专以下" value="below_college" />
                      <el-option label="大专" value="college" />
                      <el-option label="本科" value="bachelor" />
                      <el-option label="研究生" value="master" />
                      <el-option label="博士" value="phd" />
                      <el-option label="博士后" value="postdoc" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="毕业院校" :prop="`educations.${index}.school`" :rules="rules.school">
                    <el-input v-model="edu.school" placeholder="请输入毕业院校" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="毕业时间">
                    <el-date-picker
                        v-model="edu.graduationDate"
                        type="date"
                        placeholder="选择日期"
                        value-format="YYYY-MM-DD" />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-button
                  v-if="index > 0"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="removeEducation(index)"
                  class="delete-btn"
              />
            </div>
            <el-button type="primary" icon="el-icon-plus" @click="addEducation">添加学历</el-button>
          </el-tab-pane>

          <!-- 其他标签页... -->
        </el-tabs>
      </el-form>

      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">提交</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { ElMessage } from 'element-plus'

export default {
  setup() {
    const activeTab = ref('0')
    const dialogVisible = ref(false)
    const dialogTitle = ref('新建入职')
    const formRef = ref(null)

    const form = reactive({
      name: '',
      employeeId: '',
      idType: 'id_card',
      idNumber: '',
      gender: 'male',
      birthDate: '',
      phone: '',
      email: '',
      politicalStatus: 'people',
      educations: [
        { degree: '', school: '', graduationDate: '' }
      ]
    })

    const rules = reactive({
      name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
      employeeId: [{ required: true, message: '请输入员工编号', trigger: 'blur' }],
      idType: [{ required: true, message: '请选择身份证类型', trigger: 'change' }],
      idNumber: [{ required: true, message: '请输入身份证号', trigger: 'blur' }],
      phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
      degree: [{ required: true, message: '请选择学历', trigger: 'change' }],
      school: [{ required: true, message: '请输入毕业院校', trigger: 'blur' }]
    })

    const onboardingList = ref([
      {
        id: 1,
        name: '张三',
        employeeId: 'EMP001',
        position: '前端工程师',
        entryDate: '2023-05-15',
        status: '已入职'
      },
      {
        id: 2,
        name: '李四',
        employeeId: 'EMP002',
        position: '后端工程师',
        entryDate: '2023-05-20',
        status: '待入职'
      }
    ])

    const showCreateDialog = () => {
      dialogTitle.value = '新建入职'
      // 重置表单
      Object.assign(form, {
        name: '',
        employeeId: '',
        idType: 'id_card',
        idNumber: '',
        gender: 'male',
        birthDate: '',
        phone: '',
        email: '',
        politicalStatus: 'people',
        educations: [{ degree: '', school: '', graduationDate: '' }]
      })
      dialogVisible.value = true
    }

    const handleEdit = (row) => {
      dialogTitle.value = '编辑入职信息'
      // 加载数据到表单
      Object.assign(form, row)
      dialogVisible.value = true
    }

    const handleView = (row) => {
      console.log('Viewing row:', row)
    }

    const handleDelete = (row) => {
      console.log('Viewing row:', row)
    }

    const addEducation = () => {
      form.educations.push({ degree: '', school: '', graduationDate: '' })
    }

    const removeEducation = (index) => {
      form.educations.splice(index, 1)
    }

    const submitForm = async () => {
      if (!formRef.value) return

      try {
        await formRef.value.validate()
        // 实际项目调用API保存数据
        ElMessage.success(dialogTitle.value + '成功')
        dialogVisible.value = false
      } catch (error) {
        ElMessage.error('请填写完整信息')
      }
    }

    const handleImport = () => {
      ElMessage.info('请下载模板文件，填写后上传')
      // 实际项目实现导入逻辑
    }

    const handleExport = () => {
      ElMessage.success('导出成功')
      // 实际项目实现导出逻辑
    }

    return {
      activeTab,
      dialogVisible,
      dialogTitle,
      form,
      rules,
      formRef,
      onboardingList,
      showCreateDialog,
      handleEdit,
      handleView,
      handleDelete,
      addEducation,
      removeEducation,
      submitForm,
      handleImport,
      handleExport
    }
  }
}
</script>

<style scoped>
.onboarding-container {
  padding: 20px;
}

.action-bar {
  margin-bottom: 20px;
}

.tabs-container {
  margin-top: 20px;
}

.education-item {
  position: relative;
  margin-bottom: 20px;
  padding: 15px;
  border: 1px solid #ebeef5;
  border-radius: 4px;
}

.delete-btn {
  position: absolute;
  top: 10px;
  right: 10px;
}
</style>